<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Bash 备忘清单
 &#x26;  bash cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Bash 备忘清单
===

这是开始使用 linux bash shell 脚本的快速参考备忘单。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="bash,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="..\style\style.css">
<link rel="stylesheet" href="..\style\katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="..\index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/bash.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.4.1"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="bash-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" height="1em" width="1em" viewBox="0 0 32 32">
  <path d="M28.057 6.53 17.952.532a3.8 3.8 0 0 0-3.88 0L3.965 6.53A4.03 4.03 0 0 0 2 10.002v11.996a4.03 4.03 0 0 0 1.948 3.472l10.105 5.998a3.8 3.8 0 0 0 3.88 0L28.04 25.47a4.03 4.03 0 0 0 1.948-3.472V10.002a4.03 4.03 0 0 0-1.93-3.472zM20.23 25.262v.86a.318.318 0 0 1-.148.265l-.512.293c-.08.042-.148 0-.148-.113v-.847a1.66 1.66 0 0 1-1.164.113c-.062-.042-.086-.122-.056-.2l.183-.78a.322.322 0 0 1 .102-.17.18.18 0 0 1 .05-.035.11.11 0 0 1 .08 0 1.41 1.41 0 0 0 1.059-.134 1.41 1.41 0 0 0 .79-1.21c0-.438-.24-.62-.82-.625-.734 0-1.4-.14-1.43-1.224a3.137 3.137 0 0 1 1.186-2.4v-.872a.34.34 0 0 1 .148-.268l.494-.314c.08-.042.148 0 .148.116v.872a1.61 1.61 0 0 1 .967-.116c.07.04.098.128.064.2l-.173.773a.325.325 0 0 1-.138.195c-.02.012-.05.008-.074 0a1.28 1.28 0 0 0-.931.152 1.17 1.17 0 0 0-.706 1.037c0 .395.208.515.907.53.935 0 1.337.423 1.348 1.362a3.346 3.346 0 0 1-1.228 2.53zm5.293-1.45a.201.201 0 0 1-.078.194L22.9 25.558c-.024.02-.06.023-.087.007s-.04-.05-.033-.08v-.66a.184.184 0 0 1 .116-.162l2.516-1.507c.024-.02.06-.023.087-.007s.04.05.033.08v.582zM27.288 9.06l-9.562 5.906c-1.193.706-2.07 1.478-2.07 2.914v11.778c0 .86.353 1.4.882 1.58a3.14 3.14 0 0 1-.53.053 3.13 3.13 0 0 1-1.595-.441L4.308 24.853A3.3 3.3 0 0 1 2.706 22V10.002a3.304 3.304 0 0 1 1.602-2.858l10.105-5.998c.98-.58 2.196-.58 3.176 0l10.105 5.998c.833.504 1.4 1.35 1.552 2.3-.328-.713-1.083-.9-1.962-.395h.003z" fill-rule="evenodd"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#bash-备忘清单"><span class="icon icon-link"></span></a>Bash 备忘清单</h1><div class="wrap-body">
<p>这是开始使用 linux bash shell 脚本的快速参考备忘单。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#hellosh">hello.sh</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注释">注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#参数">参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件句">条件句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#大括号扩展">大括号扩展</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#shell-执行">Shell 执行</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-参数扩展">Bash 参数扩展</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#语法">语法</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#子字符串">子字符串</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#length">Length</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#默认值">默认值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#替代-substitution">替代 Substitution</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#切片-slicing">切片 Slicing</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本路径和目录路径">基本路径和目录路径</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#transform">Transform</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-数组">Bash 数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义数组">定义数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#索引">索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#迭代-iteration">迭代 Iteration</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#with-index">With index</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#操作">操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组作为参数">数组作为参数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-字典">Bash 字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义">定义</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用字典">使用字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#迭代">迭代</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-条件句">Bash 条件句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#整数条件">整数条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串条件">字符串条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#例子">例子</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#字符串">字符串</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#组合">组合</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#相等">相等</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#正则表达式">正则表达式</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#小于">小于</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#存在">存在</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#文件条件">文件条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#更多条件">更多条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#逻辑和或">逻辑和，或</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-循环">Bash 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本-for-循环">基本 for 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类似-c-的-for-循环">类似 C 的 for 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#范围">范围</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#步长">步长</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#自动递增">自动递增</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#自动递减">自动递减</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#continue">Continue</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#break">Break</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#until">Until</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#死循环">死循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#死循环简写">死循环(简写)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#读取文件的每一行">读取文件的每一行</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-函数">Bash 函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义函数">定义函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回值">返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#抛出错误">抛出错误</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-选项">Bash 选项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#选项">选项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#全局选项">全局选项</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-历史">Bash 历史</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#命令">命令</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#表达式">表达式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#操作-1">操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#切片-slices">切片 Slices</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#杂项">杂项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数值计算">数值计算</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#子-shell">子 shell</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查命令">检查命令</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重定向">重定向</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#来源相对">来源相对</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#脚本目录">脚本目录</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#caseswitch">Case/switch</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#陷阱错误">陷阱错误</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#printf">printf</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取选项">获取选项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查命令的结果">检查命令的结果</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#grep-检查">grep 检查</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#写入文件">写入文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#特殊变量">特殊变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#反斜杠转义">反斜杠转义</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#heredoc">Heredoc</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#转到上一个目录">转到上一个目录</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#读取输入">读取输入</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件执行">条件执行</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#严格模式">严格模式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可选参数">可选参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#调试模式">调试模式</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#bash-颜色">Bash 颜色</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#颜色着色语法示例">颜色着色语法示例</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#示例">示例</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#示例-1">示例</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#样式">样式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ansi--颜色转义码">ANSI — 颜色转义码</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ansi--颜色转义码亮色">ANSI — 颜色转义码(亮色)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可用功能">可用功能</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="hellosh"><a aria-hidden="true" tabindex="-1" href="#hellosh"><span class="icon icon-link"></span></a>hello.sh</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token shebang important">#!/bin/bash</span>
</span><span class="code-line"><span class="token assign-left variable">VAR</span><span class="token operator">=</span><span class="token string">"world"</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"Hello <span class="token variable">$VAR</span>!"</span> <span class="token comment"># => Hello world!</span>
</span></code></pre>
<p>执行脚本</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">bash</span> hello.sh
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">NAME</span><span class="token operator">=</span><span class="token string">"John"</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${NAME}</span>    <span class="token comment"># => John (变量)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">$NAME</span>      <span class="token comment"># => John (变量)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">$NAME</span>"</span>    <span class="token comment"># => John (变量)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">'$NAME'</span>    <span class="token comment"># => $NAME (字符串原样输出)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">${NAME}</span>!"</span> <span class="token comment"># => John! (变量)</span>
</span><span class="code-line">NAME <span class="token operator">=</span> <span class="token string">"John"</span>   <span class="token comment"># => Error (注意不能有空格)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="注释"><a aria-hidden="true" tabindex="-1" href="#注释"><span class="icon icon-link"></span></a>注释</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 这是一个内联 Bash 注释。</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token builtin class-name">:</span> <span class="token string">'
</span></span><span class="code-line"><span class="token string">这是一个
</span></span><span class="code-line"><span class="token string">非常整洁的
</span></span><span class="code-line"><span class="token string">bash 注释
</span></span><span class="code-line"><span class="token string">'</span>
</span></code></pre>
<p>多行注释使用 <code>:'</code> 打开和 <code>'</code> 关闭</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="参数"><a aria-hidden="true" tabindex="-1" href="#参数"><span class="icon icon-link"></span></a>参数</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->

















































<table><thead><tr><th align="left">表示</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>$1</code> … <code>$9</code></td><td>参数 1 ... 9</td></tr><tr><td align="left"><code>$0</code></td><td>脚本本身的名称</td></tr><tr><td align="left"><code>$1</code></td><td>第一个参数</td></tr><tr><td align="left"><code>${10}</code></td><td>位置参数 10</td></tr><tr><td align="left"><code>$#</code></td><td>参数数量</td></tr><tr><td align="left"><code>$$</code></td><td>shell 的进程 id</td></tr><tr><td align="left"><code>$*</code></td><td>所有参数</td></tr><tr><td align="left"><code>$@</code></td><td>所有参数，从第一个开始</td></tr><tr><td align="left"><code>$-</code></td><td>当前选项</td></tr><tr><td align="left"><code>$_</code></td><td>上一个命令的最后一个参数</td></tr></tbody></table>
<p>见：<a href="http://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables">特殊参数</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="函数"><a aria-hidden="true" tabindex="-1" href="#函数"><span class="icon icon-link"></span></a>函数</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function-name function">get_name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"John"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"You are <span class="token variable"><span class="token variable">$(</span>get_name<span class="token variable">)</span></span>"</span>
</span></code></pre>
<p>见：<a href="#bash-%E5%87%BD%E6%95%B0">函数</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件句"><a aria-hidden="true" tabindex="-1" href="#条件句"><span class="icon icon-link"></span></a>条件句</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token parameter variable">-z</span> <span class="token string">"<span class="token variable">$string</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"String is empty"</span>
</span><span class="code-line"><span class="token keyword">elif</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token parameter variable">-n</span> <span class="token string">"<span class="token variable">$string</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"String is not empty"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
<p>见：<a href="#bash-%E6%9D%A1%E4%BB%B6%E5%8F%A5">条件句</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="大括号扩展"><a aria-hidden="true" tabindex="-1" href="#大括号扩展"><span class="icon icon-link"></span></a>大括号扩展</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token punctuation">{</span>A,B<span class="token punctuation">}</span>.js
</span></code></pre>
<hr>





















<table><thead><tr><th align="left">表示</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>{A,B}</code></td><td>与 <code>A B</code> 相同</td></tr><tr><td align="left"><code>{A,B}.js</code></td><td>与 <code>A.js B.js</code> 相同</td></tr><tr><td align="left"><code>{1..5}</code></td><td>与 <code>1 2 3 4 5</code> 相同</td></tr></tbody></table>
<p>见：<a href="http://wiki.bash-hackers.org/syntax/expansion/brace">大括号扩展</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="shell-执行"><a aria-hidden="true" tabindex="-1" href="#shell-执行"><span class="icon icon-link"></span></a>Shell 执行</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># => I'm in /当前/的/路径</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"I'm in <span class="token variable"><span class="token variable">$(</span><span class="token environment constant">PWD</span><span class="token variable">)</span></span>"</span>
</span><span class="code-line"><span class="token comment"># Same as:</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"I'm in <span class="token variable"><span class="token variable">`</span><span class="token builtin class-name">pwd</span><span class="token variable">`</span></span>"</span>
</span></code></pre>
<p>见：<a href="http://wiki.bash-hackers.org/syntax/expansion/cmdsubst">命令替换</a></p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-参数扩展"><a aria-hidden="true" tabindex="-1" href="#bash-参数扩展"><span class="icon icon-link"></span></a>Bash 参数扩展</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="语法"><a aria-hidden="true" tabindex="-1" href="#语法"><span class="icon icon-link"></span></a>语法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->









































<table><thead><tr><th align="left">代码</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>${FOO%suffix}</code></td><td>删除后缀</td></tr><tr><td align="left"><code>${FOO#prefix}</code></td><td>删除前缀</td></tr><tr><td align="left"><code>${FOO%%suffix}</code></td><td>去掉长后缀</td></tr><tr><td align="left"><code>${FOO##prefix}</code></td><td>删除长前缀</td></tr><tr><td align="left"><code>${FOO/from/to}</code></td><td>替换第一个匹配项</td></tr><tr><td align="left"><code>${FOO//from/to}</code></td><td>全部替换</td></tr><tr><td align="left"><code>${FOO/%from/to}</code></td><td>替换后缀</td></tr><tr><td align="left"><code>${FOO/#from/to}</code></td><td>替换前缀</td></tr></tbody></table>
<h4 id="子字符串"><a aria-hidden="true" tabindex="-1" href="#子字符串"><span class="icon icon-link"></span></a>子字符串</h4>

















<table><thead><tr><th align="left">表示</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>${FOO:0:3}</code></td><td>子串 <em>(位置，长度)</em></td></tr><tr><td align="left"><code>${FOO:(-3):3}</code></td><td>从右边开始的子串</td></tr></tbody></table>
<h4 id="length"><a aria-hidden="true" tabindex="-1" href="#length"><span class="icon icon-link"></span></a>Length</h4>













<table><thead><tr><th align="left">表示</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>${#FOO}</code></td><td><code>$FOO</code> 的长度</td></tr></tbody></table>
<h4 id="默认值"><a aria-hidden="true" tabindex="-1" href="#默认值"><span class="icon icon-link"></span></a>默认值</h4>

























<table><thead><tr><th align="left">表示</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>${FOO:-val}</code></td><td><code>$FOO</code>，如果未设置，则为 <code>val</code></td></tr><tr><td align="left"><code>${FOO:=val}</code></td><td>如果未设置，则将 <code>$FOO</code> 设置为 <code>val</code></td></tr><tr><td align="left"><code>${FOO:+val}</code></td><td><code>val</code> 如果设置了<code>$FOO</code></td></tr><tr><td align="left"><code>${FOO:?message}</code></td><td>如果 <code>$FOO</code> 未设置，则显示消息并退出</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="替代-substitution"><a aria-hidden="true" tabindex="-1" href="#替代-substitution"><span class="icon icon-link"></span></a>替代 Substitution</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${food<span class="token operator">:-</span>Cake}</span>  <span class="token comment">#=> $food or "Cake"</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">STR</span><span class="token operator">=</span><span class="token string">"/path/to/foo.cpp"</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">%</span>.cpp}</span>    <span class="token comment"># /path/to/foo</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">%</span>.cpp}</span>.o  <span class="token comment"># /path/to/foo.o</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">%</span><span class="token operator">/</span>*}</span>      <span class="token comment"># /path/to</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">##</span>*.}</span>     <span class="token comment"># cpp (extension)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">##</span>*<span class="token operator">/</span>}</span>     <span class="token comment"># foo.cpp (basepath)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">#</span>*<span class="token operator">/</span>}</span>      <span class="token comment"># path/to/foo.cpp</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">##</span>*<span class="token operator">/</span>}</span>     <span class="token comment"># foo.cpp</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">/</span>foo<span class="token operator">/</span>bar}</span> <span class="token comment"># /path/to/bar.cpp</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="切片-slicing"><a aria-hidden="true" tabindex="-1" href="#切片-slicing"><span class="icon icon-link"></span></a>切片 Slicing</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">name</span><span class="token operator">=</span><span class="token string">"John"</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name}</span>           <span class="token comment"># => John</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span>0<span class="token operator">:</span>2}</span>       <span class="token comment"># => Jo</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span><span class="token operator">:</span>2}</span>        <span class="token comment"># => Jo</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span><span class="token operator">:-</span>1}</span>       <span class="token comment"># => Joh</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span>(-1)}</span>      <span class="token comment"># => n</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span>(-2)}</span>      <span class="token comment"># => hn</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span>(-2)<span class="token operator">:</span>2}</span>    <span class="token comment"># => hn</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token assign-left variable">length</span><span class="token operator">=</span><span class="token number">2</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${name<span class="token operator">:</span>0<span class="token operator">:</span>length}</span>  <span class="token comment"># => Jo</span>
</span></code></pre>
<p>见：<a href="http://wiki.bash-hackers.org/syntax/pe">参数扩展</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基本路径和目录路径"><a aria-hidden="true" tabindex="-1" href="#基本路径和目录路径"><span class="icon icon-link"></span></a>基本路径和目录路径</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">SRC</span><span class="token operator">=</span><span class="token string">"/path/to/foo.cpp"</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">BASEPATH</span><span class="token operator">=</span><span class="token variable">${SRC<span class="token operator">##</span>*<span class="token operator">/</span>}</span>   
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">$BASEPATH</span>  <span class="token comment"># => "foo.cpp"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token assign-left variable">DIRPATH</span><span class="token operator">=</span><span class="token variable">${SRC<span class="token operator">%</span>$BASEPATH}</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">$DIRPATH</span>   <span class="token comment"># => "/path/to/"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="transform"><a aria-hidden="true" tabindex="-1" href="#transform"><span class="icon icon-link"></span></a>Transform</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">STR</span><span class="token operator">=</span><span class="token string">"HELLO WORLD!"</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">,</span>}</span>      <span class="token comment"># => hELLO WORLD!</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">,,</span>}</span>     <span class="token comment"># => hello world!</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token assign-left variable">STR</span><span class="token operator">=</span><span class="token string">"hello world!"</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">^</span>}</span>      <span class="token comment"># => Hello world!</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${STR<span class="token operator">^^</span>}</span>     <span class="token comment"># => HELLO WORLD!</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token assign-left variable">ARR</span><span class="token operator">=</span><span class="token punctuation">(</span>hello World<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">${ARR<span class="token punctuation">[</span>@<span class="token punctuation">]</span><span class="token operator">,</span>}</span>"</span> <span class="token comment"># => hello world</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">${ARR<span class="token punctuation">[</span>@<span class="token punctuation">]</span><span class="token operator">^</span>}</span>"</span> <span class="token comment"># => Hello World</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-数组"><a aria-hidden="true" tabindex="-1" href="#bash-数组"><span class="icon icon-link"></span></a>Bash 数组</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义数组"><a aria-hidden="true" tabindex="-1" href="#定义数组"><span class="icon icon-link"></span></a>定义数组</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">'Apple'</span> <span class="token string">'Banana'</span> <span class="token string">'Orange'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">Fruits<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"Apple"</span>
</span><span class="code-line">Fruits<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"Banana"</span>
</span><span class="code-line">Fruits<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"Orange"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token assign-left variable">ARRAY1</span><span class="token operator">=</span><span class="token punctuation">(</span>foo<span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment"># => foo1 foo2</span>
</span><span class="code-line"><span class="token assign-left variable">ARRAY2</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token punctuation">{</span>A<span class="token punctuation">..</span>D<span class="token punctuation">}</span><span class="token punctuation">)</span>    <span class="token comment"># => A B C D</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 合并 => foo1 foo2 A B C D</span>
</span><span class="code-line"><span class="token assign-left variable">ARRAY3</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token variable">${ARRAY1<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span> <span class="token variable">${ARRAY2<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 声明构造</span>
</span><span class="code-line"><span class="token builtin class-name">declare</span> <span class="token parameter variable">-a</span> <span class="token assign-left variable">Numbers</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token assign-left variable">Numbers</span><span class="token operator">+=</span><span class="token punctuation">(</span><span class="token number">4</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment"># 附加 => 1 2 3 4 5</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="索引"><a aria-hidden="true" tabindex="-1" href="#索引"><span class="icon icon-link"></span></a>索引</h3><div class="wrap-body">













































<table><thead><tr><th align="left">:-</th><th>-</th></tr></thead><tbody><tr><td align="left"><code>${Fruits[0]}</code></td><td>第一个元素</td></tr><tr><td align="left"><code>${Fruits[-1]}</code></td><td>最后一个元素</td></tr><tr><td align="left"><code>${Fruits[*]}</code></td><td>所有元素</td></tr><tr><td align="left"><code>${Fruits[@]}</code></td><td>所有元素</td></tr><tr><td align="left"><code>${#Fruits[@]}</code></td><td>总数</td></tr><tr><td align="left"><code>${#Fruits}</code></td><td>第一节长度</td></tr><tr><td align="left"><code>${#Fruits[3]}</code></td><td>第n个长度</td></tr><tr><td align="left"><code>${Fruits[@]:3:2}</code></td><td>范围</td></tr><tr><td align="left"><code>${!Fruits[@]}</code></td><td>所有 Key</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="迭代-iteration"><a aria-hidden="true" tabindex="-1" href="#迭代-iteration"><span class="icon icon-link"></span></a>迭代 Iteration</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">'Apple'</span> <span class="token string">'Banana'</span> <span class="token string">'Orange'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">e</span> <span class="token keyword">in</span> <span class="token string">"<span class="token variable">${Fruits<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$e</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
<h4 id="with-index"><a aria-hidden="true" tabindex="-1" href="#with-index"><span class="icon icon-link"></span></a>With index</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token string">"<span class="token variable">${<span class="token operator">!</span>Fruits<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token builtin class-name">printf</span> <span class="token string">"%s<span class="token entity" title="\t">\t</span>%s<span class="token entity" title="\n">\n</span>"</span> <span class="token string">"<span class="token variable">$i</span>"</span> <span class="token string">"<span class="token variable">${Fruits<span class="token punctuation">[</span>$i<span class="token punctuation">]</span>}</span>"</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="操作"><a aria-hidden="true" tabindex="-1" href="#操作"><span class="icon icon-link"></span></a>操作</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"<span class="token variable">${Fruits<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span> <span class="token string">"Watermelon"</span><span class="token punctuation">)</span>         <span class="token comment"># 添加</span>
</span><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">+=</span><span class="token punctuation">(</span><span class="token string">'Watermelon'</span><span class="token punctuation">)</span>                       <span class="token comment"># 也是添加</span>
</span><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span> <span class="token variable">${Fruits<span class="token punctuation">[</span>@<span class="token punctuation">]</span><span class="token operator">/</span>Ap*<span class="token operator">/</span>}</span> <span class="token punctuation">)</span>                 <span class="token comment"># 通过正则表达式匹配删除</span>
</span><span class="code-line"><span class="token builtin class-name">unset</span> Fruits<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>                              <span class="token comment"># 删除一项</span>
</span><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"<span class="token variable">${Fruits<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span><span class="token punctuation">)</span>                      <span class="token comment"># 复制</span>
</span><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"<span class="token variable">${Fruits<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span> <span class="token string">"<span class="token variable">${Veggies<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span><span class="token punctuation">)</span>      <span class="token comment"># 连接</span>
</span><span class="code-line"><span class="token assign-left variable">lines</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token variable"><span class="token variable">`</span><span class="token function">cat</span> <span class="token string">"logfile"</span><span class="token variable">`</span></span><span class="token punctuation">)</span>                      <span class="token comment"># 从文件中读取</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组作为参数"><a aria-hidden="true" tabindex="-1" href="#数组作为参数"><span class="icon icon-link"></span></a>数组作为参数</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">function</span> <span class="token function-name function">extract</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token builtin class-name">local</span> <span class="token parameter variable">-n</span> <span class="token assign-left variable">myarray</span><span class="token operator">=</span><span class="token variable">$1</span>
</span><span class="code-line">  <span class="token builtin class-name">local</span> <span class="token assign-left variable">idx</span><span class="token operator">=</span><span class="token variable">$2</span>
</span><span class="code-line">  <span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">${myarray<span class="token punctuation">[</span>$idx<span class="token punctuation">]</span>}</span>"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token assign-left variable">Fruits</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">'Apple'</span> <span class="token string">'Banana'</span> <span class="token string">'Orange'</span><span class="token punctuation">)</span>
</span><span class="code-line">extract Fruits <span class="token number">2</span>     <span class="token comment"># => Orangle</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-字典"><a aria-hidden="true" tabindex="-1" href="#bash-字典"><span class="icon icon-link"></span></a>Bash 字典</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义"><a aria-hidden="true" tabindex="-1" href="#定义"><span class="icon icon-link"></span></a>定义</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">declare</span> <span class="token parameter variable">-A</span> sounds
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">sounds<span class="token punctuation">[</span>dog<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"bark"</span>
</span><span class="code-line">sounds<span class="token punctuation">[</span>cow<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"moo"</span>
</span><span class="code-line">sounds<span class="token punctuation">[</span>bird<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"tweet"</span>
</span><span class="code-line">sounds<span class="token punctuation">[</span>wolf<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"howl"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用字典"><a aria-hidden="true" tabindex="-1" href="#使用字典"><span class="icon icon-link"></span></a>使用字典</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${sounds<span class="token punctuation">[</span>dog<span class="token punctuation">]</span>}</span> <span class="token comment"># Dog's sound</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${sounds<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>   <span class="token comment"># All values</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${<span class="token operator">!</span>sounds<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>  <span class="token comment"># All keys</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">${<span class="token operator">#</span>sounds<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>  <span class="token comment"># Number of elements</span>
</span><span class="code-line"><span class="token builtin class-name">unset</span> sounds<span class="token punctuation">[</span>dog<span class="token punctuation">]</span>   <span class="token comment"># Delete dog</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="迭代"><a aria-hidden="true" tabindex="-1" href="#迭代"><span class="icon icon-link"></span></a>迭代</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">val</span> <span class="token keyword">in</span> <span class="token string">"<span class="token variable">${sounds<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$val</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
<hr>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">key</span> <span class="token keyword">in</span> <span class="token string">"<span class="token variable">${<span class="token operator">!</span>sounds<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$key</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-条件句"><a aria-hidden="true" tabindex="-1" href="#bash-条件句"><span class="icon icon-link"></span></a>Bash 条件句</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="整数条件"><a aria-hidden="true" tabindex="-1" href="#整数条件"><span class="icon icon-link"></span></a>整数条件</h3><div class="wrap-body">

















































<table><thead><tr><th align="left">条件</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>[[ NUM -eq NUM ]]</code></td><td>等于 <yel>Eq</yel>ual</td></tr><tr><td align="left"><code>[[ NUM -ne NUM ]]</code></td><td>不等于 <yel>N</yel>ot <yel>e</yel>qual</td></tr><tr><td align="left"><code>[[ NUM -lt NUM ]]</code></td><td>小于 <yel>L</yel>ess <yel>t</yel>han</td></tr><tr><td align="left"><code>[[ NUM -le NUM ]]</code></td><td>小于等于 <yel>L</yel>ess than or <yel>e</yel>qual</td></tr><tr><td align="left"><code>[[ NUM -gt NUM ]]</code></td><td>大于 <yel>G</yel>reater <yel>t</yel>han</td></tr><tr><td align="left"><code>[[ NUM -ge NUM ]]</code></td><td>大于等于 <yel>G</yel>reater than or <yel>e</yel>qual</td></tr><tr><td align="left"><code>(( NUM &#x3C; NUM ))</code></td><td>小于</td></tr><tr><td align="left"><code>(( NUM &#x3C;= NUM ))</code></td><td>小于或等于</td></tr><tr><td align="left"><code>(( NUM > NUM ))</code></td><td>比...更大</td></tr><tr><td align="left"><code>(( NUM >= NUM ))</code></td><td>大于等于</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串条件"><a aria-hidden="true" tabindex="-1" href="#字符串条件"><span class="icon icon-link"></span></a>字符串条件</h3><div class="wrap-body">









































<table><thead><tr><th align="left">条件</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>[[ -z STR ]]</code></td><td>空字符串</td></tr><tr><td align="left"><code>[[ -n STR ]]</code></td><td><yel>非</yel>空字符串</td></tr><tr><td align="left"><code>[[ STR == STR ]]</code></td><td>相等</td></tr><tr><td align="left"><code>[[ STR = STR ]]</code></td><td>相等(同上)</td></tr><tr><td align="left"><code>[[ STR &#x3C; STR ]]</code></td><td>小于 <em>(ASCII)</em></td></tr><tr><td align="left"><code>[[ STR > STR ]]</code></td><td>大于 <em>(ASCII)</em></td></tr><tr><td align="left"><code>[[ STR != STR ]]</code></td><td>不相等</td></tr><tr><td align="left"><code>[[ STR =~ STR ]]</code></td><td>正则表达式</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="例子"><a aria-hidden="true" tabindex="-1" href="#例子"><span class="icon icon-link"></span></a>例子</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<h4 id="字符串"><a aria-hidden="true" tabindex="-1" href="#字符串"><span class="icon icon-link"></span></a>字符串</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token parameter variable">-z</span> <span class="token string">"<span class="token variable">$string</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"String is empty"</span>
</span><span class="code-line"><span class="token keyword">elif</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token parameter variable">-n</span> <span class="token string">"<span class="token variable">$string</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"String is not empty"</span>
</span><span class="code-line"><span class="token keyword">else</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"This never happens"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
<h4 id="组合"><a aria-hidden="true" tabindex="-1" href="#组合"><span class="icon icon-link"></span></a>组合</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> X <span class="token operator">&#x26;&#x26;</span> Y <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token punctuation">..</span>.
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
<h4 id="相等"><a aria-hidden="true" tabindex="-1" href="#相等"><span class="icon icon-link"></span></a>相等</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$A</span>"</span> <span class="token operator">==</span> <span class="token string">"<span class="token variable">$B</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token punctuation">..</span>.
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
<h4 id="正则表达式"><a aria-hidden="true" tabindex="-1" href="#正则表达式"><span class="icon icon-link"></span></a>正则表达式</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token string">'1. abc'</span> <span class="token operator">=~</span> <span class="token punctuation">(</span><span class="token punctuation">[</span>a-z<span class="token punctuation">]</span>+<span class="token punctuation">)</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">${<span class="token environment constant">BASH_REMATCH</span><span class="token punctuation">[</span>1<span class="token punctuation">]</span>}</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
<h4 id="小于"><a aria-hidden="true" tabindex="-1" href="#小于"><span class="icon icon-link"></span></a>小于</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token variable"><span class="token punctuation">((</span> $a <span class="token operator">&#x3C;</span> $b <span class="token punctuation">))</span></span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">   <span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">$a</span> is smaller than <span class="token variable">$b</span>"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
<h4 id="存在"><a aria-hidden="true" tabindex="-1" href="#存在"><span class="icon icon-link"></span></a>存在</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token parameter variable">-e</span> <span class="token string">"file.txt"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"file exists"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="文件条件"><a aria-hidden="true" tabindex="-1" href="#文件条件"><span class="icon icon-link"></span></a>文件条件</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->





















































<table><thead><tr><th align="left">条件</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>[[ -e FILE ]]</code></td><td>存在</td></tr><tr><td align="left"><code>[[ -d FILE ]]</code></td><td>目录</td></tr><tr><td align="left"><code>[[ -f FILE ]]</code></td><td>文件</td></tr><tr><td align="left"><code>[[ -h FILE ]]</code></td><td>符号链接</td></tr><tr><td align="left"><code>[[ -s FILE ]]</code></td><td>大小 > 0 字节</td></tr><tr><td align="left"><code>[[ -r FILE ]]</code></td><td>可读</td></tr><tr><td align="left"><code>[[ -w FILE ]]</code></td><td>可写</td></tr><tr><td align="left"><code>[[ -x FILE ]]</code></td><td>可执行文件</td></tr><tr><td align="left"><code>[[ f1 -nt f2 ]]</code></td><td>f1 比 f2 新</td></tr><tr><td align="left"><code>[[ f1 -ot f2 ]]</code></td><td>f2 比 f1 新</td></tr><tr><td align="left"><code>[[ f1 -ef f2 ]]</code></td><td>相同的文件</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="更多条件"><a aria-hidden="true" tabindex="-1" href="#更多条件"><span class="icon icon-link"></span></a>更多条件</h3><div class="wrap-body">

























<table><thead><tr><th align="left">条件</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>[[ -o noclobber ]]</code></td><td>如果启用 OPTION</td></tr><tr><td align="left"><code>[[ ! EXPR ]]</code></td><td>不是 Not</td></tr><tr><td align="left"><code>[[ X &#x26;&#x26; Y ]]</code></td><td>和 And</td></tr><tr><td align="left"><code>[[ X || Y ]]</code></td><td>或者 Or</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="逻辑和或"><a aria-hidden="true" tabindex="-1" href="#逻辑和或"><span class="icon icon-link"></span></a>逻辑和，或</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">=</span> <span class="token string">'y'</span> <span class="token parameter variable">-a</span> <span class="token variable">$2</span> <span class="token parameter variable">-gt</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"yes"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">=</span> <span class="token string">'n'</span> <span class="token parameter variable">-o</span> <span class="token variable">$2</span> <span class="token parameter variable">-lt</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"no"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-循环"><a aria-hidden="true" tabindex="-1" href="#bash-循环"><span class="icon icon-link"></span></a>Bash 循环</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基本-for-循环"><a aria-hidden="true" tabindex="-1" href="#基本-for-循环"><span class="icon icon-link"></span></a>基本 for 循环</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> /etc/rc.*<span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类似-c-的-for-循环"><a aria-hidden="true" tabindex="-1" href="#类似-c-的-for-循环"><span class="icon icon-link"></span></a>类似 C 的 for 循环</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span>i <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> i <span class="token operator">&#x3C;</span> <span class="token number">100</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">))</span></span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="范围"><a aria-hidden="true" tabindex="-1" href="#范围"><span class="icon icon-link"></span></a>范围</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"Welcome <span class="token variable">$i</span>"</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
<h4 id="步长"><a aria-hidden="true" tabindex="-1" href="#步长"><span class="icon icon-link"></span></a>步长</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token punctuation">{</span><span class="token number">5</span><span class="token punctuation">..</span><span class="token number">50</span><span class="token punctuation">..</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"Welcome <span class="token variable">$i</span>"</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="自动递增"><a aria-hidden="true" tabindex="-1" href="#自动递增"><span class="icon icon-link"></span></a>自动递增</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">i</span><span class="token operator">=</span><span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">while</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$i</span> <span class="token parameter variable">-lt</span> <span class="token number">4</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"Number: <span class="token variable">$i</span>"</span>
</span><span class="code-line">    <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">++</span><span class="token punctuation">))</span></span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="自动递减"><a aria-hidden="true" tabindex="-1" href="#自动递减"><span class="icon icon-link"></span></a>自动递减</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">i</span><span class="token operator">=</span><span class="token number">3</span>
</span><span class="code-line"><span class="token keyword">while</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$i</span> <span class="token parameter variable">-gt</span> <span class="token number">0</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"Number: <span class="token variable">$i</span>"</span>
</span><span class="code-line">    <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">--</span><span class="token punctuation">))</span></span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="continue"><a aria-hidden="true" tabindex="-1" href="#continue"><span class="icon icon-link"></span></a>Continue</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">number</span> <span class="token keyword">in</span> <span class="token variable"><span class="token variable">$(</span><span class="token function">seq</span> <span class="token number">1</span> <span class="token number">3</span><span class="token variable">)</span></span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$number</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">        <span class="token builtin class-name">continue</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">fi</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">$number</span>"</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="break"><a aria-hidden="true" tabindex="-1" href="#break"><span class="icon icon-link"></span></a>Break</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token for-or-select variable">number</span> <span class="token keyword">in</span> <span class="token variable"><span class="token variable">$(</span><span class="token function">seq</span> <span class="token number">1</span> <span class="token number">3</span><span class="token variable">)</span></span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$number</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">        <span class="token comment"># Skip entire rest of loop.</span>
</span><span class="code-line">        <span class="token builtin class-name">break</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token keyword">fi</span>
</span><span class="code-line">    <span class="token comment"># This will only print 1</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">$number</span>"</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="until"><a aria-hidden="true" tabindex="-1" href="#until"><span class="icon icon-link"></span></a>Until</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">count</span><span class="token operator">=</span><span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">until</span> <span class="token punctuation">[</span> <span class="token variable">$count</span> <span class="token parameter variable">-gt</span> <span class="token number">10</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">$count</span>"</span>
</span><span class="code-line">    <span class="token variable"><span class="token punctuation">((</span>count<span class="token operator">++</span><span class="token punctuation">))</span></span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="死循环"><a aria-hidden="true" tabindex="-1" href="#死循环"><span class="icon icon-link"></span></a>死循环</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">while</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token comment"># here is some code.</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="死循环简写"><a aria-hidden="true" tabindex="-1" href="#死循环简写"><span class="icon icon-link"></span></a>死循环(简写)</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">while</span> <span class="token builtin class-name">:</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token comment"># here is some code.</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="读取文件的每一行"><a aria-hidden="true" tabindex="-1" href="#读取文件的每一行"><span class="icon icon-link"></span></a>读取文件的每一行</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">cat</span> file.txt <span class="token operator">|</span> <span class="token keyword">while</span> <span class="token builtin class-name">read</span> line<span class="token punctuation">;</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$line</span>
</span><span class="code-line"><span class="token keyword">done</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-函数"><a aria-hidden="true" tabindex="-1" href="#bash-函数"><span class="icon icon-link"></span></a>Bash 函数</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义函数"><a aria-hidden="true" tabindex="-1" href="#定义函数"><span class="icon icon-link"></span></a>定义函数</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function-name function">myfunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token builtin class-name">echo</span> <span class="token string">"hello <span class="token variable">$1</span>"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 同上(替代语法)</span>
</span><span class="code-line"><span class="token keyword">function</span> <span class="token function-name function">myfunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token builtin class-name">echo</span> <span class="token string">"hello <span class="token variable">$1</span>"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">myfunc <span class="token string">"John"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="返回值"><a aria-hidden="true" tabindex="-1" href="#返回值"><span class="icon icon-link"></span></a>返回值</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function-name function">myfunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token builtin class-name">local</span> <span class="token assign-left variable">myresult</span><span class="token operator">=</span><span class="token string">'some value'</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$myresult</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">result</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span>myfunc<span class="token variable">)</span></span>"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="抛出错误"><a aria-hidden="true" tabindex="-1" href="#抛出错误"><span class="icon icon-link"></span></a>抛出错误</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function-name function">myfunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token builtin class-name">return</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> myfunc<span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"success"</span>
</span><span class="code-line"><span class="token keyword">else</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"failure"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-选项"><a aria-hidden="true" tabindex="-1" href="#bash-选项"><span class="icon icon-link"></span></a>Bash 选项</h2><div class="wrap-body">
<!--rehype:body-class=cols-2-->
</div></div><div class="h2wrap-body cols-2"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="选项"><a aria-hidden="true" tabindex="-1" href="#选项"><span class="icon icon-link"></span></a>选项</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 避免覆盖文件</span>
</span><span class="code-line"><span class="token comment"># (echo "hi" > foo)</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-o</span> noclobber
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 用于出错时退出</span>
</span><span class="code-line"><span class="token comment"># 避免级联错误</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-o</span> errexit   
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 揭示隐藏的失败</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-o</span> pipefail  
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 公开未设置的变量</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-o</span> nounset
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="全局选项"><a aria-hidden="true" tabindex="-1" href="#全局选项"><span class="icon icon-link"></span></a>全局选项</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 不匹配的 glob 被删除</span>
</span><span class="code-line"><span class="token comment"># ('*.foo' => '')</span>
</span><span class="code-line"><span class="token builtin class-name">shopt</span> <span class="token parameter variable">-s</span> nullglob   
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 不匹配的 glob 抛出错误</span>
</span><span class="code-line"><span class="token builtin class-name">shopt</span> <span class="token parameter variable">-s</span> failglob  
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 不区分大小写的球体</span>
</span><span class="code-line"><span class="token builtin class-name">shopt</span> <span class="token parameter variable">-s</span> nocaseglob 
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 通配符匹配点文件</span>
</span><span class="code-line"><span class="token comment"># ("*.sh" => ".foo.sh")</span>
</span><span class="code-line"><span class="token builtin class-name">shopt</span> <span class="token parameter variable">-s</span> dotglob    
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 允许 ** 进行递归匹配</span>
</span><span class="code-line"><span class="token comment"># ('lib/**/*.rb' => 'lib/a/b/c.rb')</span>
</span><span class="code-line"><span class="token builtin class-name">shopt</span> <span class="token parameter variable">-s</span> globstar   
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-历史"><a aria-hidden="true" tabindex="-1" href="#bash-历史"><span class="icon icon-link"></span></a>Bash 历史</h2><div class="wrap-body">
<!--rehype:body-class=cols-2-->
</div></div><div class="h2wrap-body cols-2"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="命令"><a aria-hidden="true" tabindex="-1" href="#命令"><span class="icon icon-link"></span></a>命令</h3><div class="wrap-body">





















<table><thead><tr><th align="left">命令</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>history</code></td><td>显示历史</td></tr><tr><td align="left"><code>sudo !!</code></td><td>使用 sudo 运行上一个命令</td></tr><tr><td align="left"><code>shopt -s histverify</code></td><td>不要立即执行扩展结果</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="表达式"><a aria-hidden="true" tabindex="-1" href="#表达式"><span class="icon icon-link"></span></a>表达式</h3><div class="wrap-body">





























<table><thead><tr><th align="left">表达式</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>!$</code></td><td>展开最新命令的最后一个参数</td></tr><tr><td align="left"><code>!*</code></td><td>展开最新命令的所有参数</td></tr><tr><td align="left"><code>!-n</code></td><td>展开第 <code>n</code> 个最近的命令</td></tr><tr><td align="left"><code>!n</code></td><td>展开历史中的第 <code>n</code> 个命令</td></tr><tr><td align="left"><code>!&#x3C;command></code></td><td>展开最近调用的命令 <code>&#x3C;command></code></td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="操作-1"><a aria-hidden="true" tabindex="-1" href="#操作-1"><span class="icon icon-link"></span></a>操作</h3><div class="wrap-body">





























<table><thead><tr><th align="left">代码</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>!!</code></td><td>再次执行最后一条命令</td></tr><tr><td align="left"><code>!!:s/&#x3C;FROM>/&#x3C;TO>/</code></td><td>在最近的命令中将第一次出现的 <code>&#x3C;FROM></code> 替换为 <code>&#x3C;TO></code></td></tr><tr><td align="left"><code>!!:gs/&#x3C;FROM>/&#x3C;TO>/</code></td><td>在最近的命令中将所有出现的 <code>&#x3C;FROM></code> 替换为 <code>&#x3C;TO></code></td></tr><tr><td align="left"><code>!$:t</code></td><td>仅从最近命令的最后一个参数扩展基本名称</td></tr><tr><td align="left"><code>!$:h</code></td><td>仅从最近命令的最后一个参数展开目录</td></tr></tbody></table>
<p><code>!!</code> 和 <code>!$</code> 可以替换为任何有效的扩展。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="切片-slices"><a aria-hidden="true" tabindex="-1" href="#切片-slices"><span class="icon icon-link"></span></a>切片 Slices</h3><div class="wrap-body">





























<table><thead><tr><th align="left">代码</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>!!:n</code></td><td>仅扩展最近命令中的第 <code>n</code> 个标记(命令为 <code>0</code>；第一个参数为 <code>1</code>)</td></tr><tr><td align="left"><code>!^</code></td><td>从最近的命令展开第一个参数</td></tr><tr><td align="left"><code>!$</code></td><td>从最近的命令中展开最后一个标记</td></tr><tr><td align="left"><code>!!:n-m</code></td><td>从最近的命令扩展令牌范围</td></tr><tr><td align="left"><code>!!:n-$</code></td><td>从最近的命令中将第 <code>n</code> 个标记展开到最后</td></tr></tbody></table>
<p><code>!!</code> 可以替换为任何有效的扩展，即 <code>!cat</code>、<code>!-2</code>、<code>!42</code> 等。</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="杂项"><a aria-hidden="true" tabindex="-1" href="#杂项"><span class="icon icon-link"></span></a>杂项</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数值计算"><a aria-hidden="true" tabindex="-1" href="#数值计算"><span class="icon icon-link"></span></a>数值计算</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token variable"><span class="token variable">$((</span>a <span class="token operator">+</span> <span class="token number">200</span><span class="token variable">))</span></span>      <span class="token comment"># Add 200 to $a</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token variable"><span class="token variable">$((</span>$RANDOM<span class="token operator">%</span><span class="token number">200</span><span class="token variable">))</span></span>  <span class="token comment"># Random number 0..199</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="子-shell"><a aria-hidden="true" tabindex="-1" href="#子-shell"><span class="icon icon-link"></span></a>子 shell</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token punctuation">(</span>cd somedir<span class="token punctuation">;</span> <span class="token builtin class-name">echo</span> <span class="token string">"I'm now in <span class="token environment constant">$PWD</span>"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token builtin class-name">pwd</span> <span class="token comment"># still in first directory</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查命令"><a aria-hidden="true" tabindex="-1" href="#检查命令"><span class="icon icon-link"></span></a>检查命令</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">command</span> <span class="token parameter variable">-V</span> <span class="token builtin class-name">cd</span>
</span><span class="code-line"><span class="token comment">#=> "cd is a function/alias/whatever"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2 col-span-2"><div class="wrap-header h3wrap"><h3 id="重定向"><a aria-hidden="true" tabindex="-1" href="#重定向"><span class="icon icon-link"></span></a>重定向</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2 col-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">python hello.py <span class="token operator">></span> output.txt   <span class="token comment"># 标准输出到(文件)</span>
</span><span class="code-line">python hello.py <span class="token operator">>></span> output.txt  <span class="token comment"># 标准输出到(文件)，追加</span>
</span><span class="code-line">python hello.py <span class="token operator"><span class="token file-descriptor important">2</span>></span> error.log   <span class="token comment"># 标准错误到(文件)</span>
</span><span class="code-line">python hello.py <span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token file-descriptor important">&#x26;1</span>           <span class="token comment"># 标准错误到标准输出</span>
</span><span class="code-line">python hello.py <span class="token operator"><span class="token file-descriptor important">2</span>></span>/dev/null    <span class="token comment"># 标准错误到(空null)</span>
</span><span class="code-line">python hello.py <span class="token operator">&#x26;></span>/dev/null    <span class="token comment"># 标准输出和标准错误到(空null)</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">python hello.py <span class="token operator">&#x3C;</span> foo.txt      <span class="token comment"># 将 foo.txt 提供给 python 的标准输入</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="来源相对"><a aria-hidden="true" tabindex="-1" href="#来源相对"><span class="icon icon-link"></span></a>来源相对</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">source</span> <span class="token string">"<span class="token variable">${0<span class="token operator">%</span><span class="token operator">/</span>*}</span>/../share/foo.sh"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="脚本目录"><a aria-hidden="true" tabindex="-1" href="#脚本目录"><span class="icon icon-link"></span></a>脚本目录</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">DIR</span><span class="token operator">=</span><span class="token string">"<span class="token variable">${0<span class="token operator">%</span><span class="token operator">/</span>*}</span>"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="caseswitch"><a aria-hidden="true" tabindex="-1" href="#caseswitch"><span class="icon icon-link"></span></a>Case/switch</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">case</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token keyword">in</span>
</span><span class="code-line">    start <span class="token operator">|</span> up<span class="token punctuation">)</span>
</span><span class="code-line">    vagrant up
</span><span class="code-line">    <span class="token punctuation">;</span><span class="token punctuation">;</span>
</span><span class="code-line">    *<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"Usage: <span class="token variable">$0</span> {start|stop|ssh}"</span>
</span><span class="code-line">    <span class="token punctuation">;</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">esac</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="陷阱错误"><a aria-hidden="true" tabindex="-1" href="#陷阱错误"><span class="icon icon-link"></span></a>陷阱错误</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">trap</span> <span class="token string">'echo Error at about $LINENO'</span> ERR
</span></code></pre>
<p>或者</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function-name function">traperr</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"ERROR: <span class="token variable">${<span class="token environment constant">BASH_SOURCE</span><span class="token punctuation">[</span>1<span class="token punctuation">]</span>}</span> at about <span class="token variable">${<span class="token environment constant">BASH_LINENO</span><span class="token punctuation">[</span>0<span class="token punctuation">]</span>}</span>"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-o</span> errtrace
</span><span class="code-line"><span class="token builtin class-name">trap</span> traperr ERR
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="printf"><a aria-hidden="true" tabindex="-1" href="#printf"><span class="icon icon-link"></span></a>printf</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">printf</span> <span class="token string">"Hello %s, I'm %s"</span> Sven Olga
</span><span class="code-line"><span class="token comment">#=> "Hello Sven, I'm Olga</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token builtin class-name">printf</span> <span class="token string">"1 + 1 = %d"</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token comment">#=> "1 + 1 = 2"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token builtin class-name">printf</span> <span class="token string">"Print a float: %f"</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token comment">#=> "Print a float: 2.000000"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="获取选项"><a aria-hidden="true" tabindex="-1" href="#获取选项"><span class="icon icon-link"></span></a>获取选项</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">while</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">=~</span> ^- <span class="token operator">&#x26;&#x26;</span> <span class="token operator">!</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">==</span> <span class="token string">"--"</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span> <span class="token keyword">case</span> <span class="token variable">$1</span> <span class="token keyword">in</span>
</span><span class="code-line">    <span class="token parameter variable">-V</span> <span class="token operator">|</span> <span class="token parameter variable">--version</span> <span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token variable">$version</span>
</span><span class="code-line">    <span class="token builtin class-name">exit</span>
</span><span class="code-line">    <span class="token punctuation">;</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token parameter variable">-s</span> <span class="token operator">|</span> <span class="token parameter variable">--string</span> <span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token builtin class-name">shift</span><span class="token punctuation">;</span> <span class="token assign-left variable">string</span><span class="token operator">=</span><span class="token variable">$1</span>
</span><span class="code-line">    <span class="token punctuation">;</span><span class="token punctuation">;</span>
</span><span class="code-line">    <span class="token parameter variable">-f</span> <span class="token operator">|</span> <span class="token parameter variable">--flag</span> <span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token assign-left variable">flag</span><span class="token operator">=</span><span class="token number">1</span>
</span><span class="code-line">    <span class="token punctuation">;</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token keyword">esac</span><span class="token punctuation">;</span> <span class="token builtin class-name">shift</span><span class="token punctuation">;</span> <span class="token keyword">done</span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token operator">==</span> <span class="token string">'--'</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span> <span class="token builtin class-name">shift</span><span class="token punctuation">;</span> <span class="token keyword">fi</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查命令的结果"><a aria-hidden="true" tabindex="-1" href="#检查命令的结果"><span class="icon icon-link"></span></a>检查命令的结果</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token function">ping</span> <span class="token parameter variable">-c</span> <span class="token number">1</span> google.com<span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"看来您的互联网连接正常"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="grep-检查"><a aria-hidden="true" tabindex="-1" href="#grep-检查"><span class="icon icon-link"></span></a>grep 检查</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token function">grep</span> <span class="token parameter variable">-q</span> <span class="token string">'foo'</span> ~/.bash_history<span class="token punctuation">;</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token builtin class-name">echo</span> <span class="token string">"您过去似乎输入过“foo”"</span>
</span><span class="code-line"><span class="token keyword">fi</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-6"><div class="wrap-header h3wrap"><h3 id="写入文件"><a aria-hidden="true" tabindex="-1" href="#写入文件"><span class="icon icon-link"></span></a>写入文件</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-6-->

















<table><thead><tr><th align="left">:--</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>cat</code></td><td>用于显示文本文件内容，全部输出</td></tr><tr><td align="left"><code>EOF</code></td><td><code>end of file</code>，表示文本结束符</td></tr></tbody></table>
<hr>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">cat</span> <span class="token operator">></span> output.txt <span class="token operator">&#x3C;&#x3C;</span><span class="token string">EOF
</span></span><span class="code-line"><span class="token string">this is test eof
</span></span><span class="code-line"><span class="token string">this is test eof2
</span></span><span class="code-line"><span class="token string">EOF</span>
</span></code></pre>
<p>输出 <code>cat output.txt</code></p>
<pre><code class="code-highlight"><span class="code-line">this is test eof
</span><span class="code-line">this is test eof2
</span></code></pre>
<p>追加内容</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">cat</span> <span class="token operator">>></span><span class="token number">2</span>.txt <span class="token operator">&#x3C;&#x3C;</span><span class="token string">EOF
</span></span><span class="code-line"><span class="token string">456
</span></span><span class="code-line"><span class="token string">789
</span></span><span class="code-line"><span class="token string">EOF</span>
</span></code></pre>
<p><code>$</code> 等特殊字符时，须利用转义字符 <code>\</code></p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">cat</span> <span class="token operator">></span> <span class="token function">file</span> <span class="token operator">&#x3C;&#x3C;</span><span class="token string">EOF
</span></span><span class="code-line"><span class="token string">export ORACLE_SID=yqpt 
</span></span><span class="code-line"><span class="token string">export PATH=\<span class="token environment constant">$PATH</span>:\<span class="token variable">$ORACLE_HOME</span>/bin  
</span></span><span class="code-line"><span class="token string">EOF</span>
</span></code></pre>
<p>取消变量替换，<code>EOF</code> 被加上双引号或者单引号，即可取消变量的替换</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">cat</span> <span class="token operator">&#x3C;&#x3C;</span> <span class="token string">"EOF"<span class="token bash punctuation"> <span class="token operator">></span> output.sh</span>
</span></span><span class="code-line"><span class="token string">echo "This is output"
</span></span><span class="code-line"><span class="token string">echo $1
</span></span><span class="code-line"><span class="token string">EOF</span>
</span></code></pre>
<p>所有TAB键将全部忽略[不能是空格]</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">cat</span> <span class="token operator">&#x3C;&#x3C;</span> <span class="token parameter variable">-EOF</span>
</span><span class="code-line">        <span class="token builtin class-name">echo</span> <span class="token string">"This is output"</span>
</span><span class="code-line">EOF
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="特殊变量"><a aria-hidden="true" tabindex="-1" href="#特殊变量"><span class="icon icon-link"></span></a>特殊变量</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->

























<table><thead><tr><th align="left">表达式</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>$?</code></td><td>最后一个任务的退出状态</td></tr><tr><td align="left"><code>$!</code></td><td>最后一个后台任务的 PID</td></tr><tr><td align="left"><code>$$</code></td><td>shell PID</td></tr><tr><td align="left"><code>$0</code></td><td>shell 脚本的文件名</td></tr></tbody></table>
<p>见<a href="http://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables">特殊参数</a>。</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="反斜杠转义"><a aria-hidden="true" tabindex="-1" href="#反斜杠转义"><span class="icon icon-link"></span></a>反斜杠转义</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<ul class="cols-4 style-none">
<li> </li>
<li>!</li>
<li>"</li>
<li>#</li>
<li>&#x26;</li>
<li>'</li>
<li>(</li>
<li>)</li>
<li>,</li>
<li>;</li>
<li>&#x3C;</li>
<li>></li>
<li>[</li>
<li>|</li>
<li>\</li>
<li>]</li>
<li>^</li>
<li>{</li>
<li>}</li>
<li>`</li>
<li>$</li>
<li>*</li>
<li>?</li>
</ul>
<!--rehype:className=cols-4 style-none-->
<p>使用 <code>\</code> 转义这些特殊字符</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="heredoc"><a aria-hidden="true" tabindex="-1" href="#heredoc"><span class="icon icon-link"></span></a>Heredoc</h3><div class="wrap-body">
<pre class="language-sh"><code class="language-sh code-highlight"><span class="code-line"><span class="token function">cat</span> <span class="token operator">&#x3C;&#x3C;</span><span class="token string">END
</span></span><span class="code-line"><span class="token string">hello world
</span></span><span class="code-line"><span class="token string">END</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="转到上一个目录"><a aria-hidden="true" tabindex="-1" href="#转到上一个目录"><span class="icon icon-link"></span></a>转到上一个目录</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">pwd</span> <span class="token comment"># /home/user/foo</span>
</span><span class="code-line"><span class="token builtin class-name">cd</span> bar/
</span><span class="code-line"><span class="token builtin class-name">pwd</span> <span class="token comment"># /home/user/foo/bar</span>
</span><span class="code-line"><span class="token builtin class-name">cd</span> -
</span><span class="code-line"><span class="token builtin class-name">pwd</span> <span class="token comment"># /home/user/foo</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="读取输入"><a aria-hidden="true" tabindex="-1" href="#读取输入"><span class="icon icon-link"></span></a>读取输入</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token parameter variable">-n</span> <span class="token string">"Proceed? [y/n]: "</span>
</span><span class="code-line"><span class="token builtin class-name">read</span> ans
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token variable">$ans</span>
</span></code></pre>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">read</span> <span class="token parameter variable">-n</span> <span class="token number">1</span> ans    <span class="token comment"># 只有一个字符</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件执行"><a aria-hidden="true" tabindex="-1" href="#条件执行"><span class="icon icon-link"></span></a>条件执行</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> commit <span class="token operator">&#x26;&#x26;</span> <span class="token function">git</span> push
</span><span class="code-line"><span class="token function">git</span> commit <span class="token operator">||</span> <span class="token builtin class-name">echo</span> <span class="token string">"Commit failed"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="严格模式"><a aria-hidden="true" tabindex="-1" href="#严格模式"><span class="icon icon-link"></span></a>严格模式</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-euo</span> pipefail
</span><span class="code-line"><span class="token assign-left variable"><span class="token environment constant">IFS</span></span><span class="token operator">=</span><span class="token string">$'<span class="token entity" title="\n">\n</span><span class="token entity" title="\t">\t</span>'</span>
</span></code></pre>
<p>参见：<a href="http://redsymbol.net/articles/unofficial-bash-strict-mode/">非官方 bash 严格模式</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="可选参数"><a aria-hidden="true" tabindex="-1" href="#可选参数"><span class="icon icon-link"></span></a>可选参数</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token assign-left variable">args</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"<span class="token variable">$@</span>"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token assign-left variable">args</span><span class="token operator">+=</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token assign-left variable">args</span><span class="token operator">+=</span><span class="token punctuation">(</span>bar<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">${args<span class="token punctuation">[</span>@<span class="token punctuation">]</span>}</span>"</span>
</span></code></pre>
<p>将参数放入数组中，然后追加</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="调试模式"><a aria-hidden="true" tabindex="-1" href="#调试模式"><span class="icon icon-link"></span></a>调试模式</h3><div class="wrap-body">
<p>启用调试模式，会把脚本中的每条命令的执行情况打印出来。它可以在整个会话或脚本上运行，也可以在脚本内以编程方式启用。</p>
<p>以调试模式运行脚本(整个脚本都会打印调试信息)：</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">bash</span> <span class="token parameter variable">-x</span> myscript.sh
</span></code></pre>
<p>在bash脚本中打开调试(针对部分内容打印调试信息)。</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token shebang important">#!/bin/bash</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> <span class="token parameter variable">-x</span>   <span class="token comment"># Enable debugging</span>
</span><span class="code-line"><span class="token comment"># some code here</span>
</span><span class="code-line"><span class="token builtin class-name">set</span> +x   <span class="token comment"># Disable debugging output.</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="bash-颜色"><a aria-hidden="true" tabindex="-1" href="#bash-颜色"><span class="icon icon-link"></span></a>Bash 颜色</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="颜色着色语法示例"><a aria-hidden="true" tabindex="-1" href="#颜色着色语法示例"><span class="icon icon-link"></span></a>颜色着色语法示例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>您可以通过为其输出着色来使您的 BASH 脚本更漂亮，使用以下模板编写彩色文本：</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"<span class="token entity" title="\e">\e</span>[COLORm文字变色了<span class="token entity" title="\e">\e</span>[0m"</span>
</span></code></pre>
<h4 id="示例"><a aria-hidden="true" tabindex="-1" href="#示例"><span class="icon icon-link"></span></a>示例</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"<span class="token entity" title="\e">\e</span>[31m命令行中显示红色文字<span class="token entity" title="\e">\e</span>[0m"</span>
</span></code></pre>
<p><red>命令行中显示红色文字</red></p>
<pre><code class="code-highlight"><span class="code-line">$ echo -e "\e[42m绿色背景\e[0m"
</span></code></pre>
<p><code style="background:green;color: black;">绿色背景</code><!--rehype:style=background:green;color: black;--></p>

























<table class="left-align"><thead><tr><th align="left">Option</th><th>Description</th></tr></thead><tbody><tr><td align="left"><code>-e</code></td><td>启用反斜杠转义的解释</td></tr><tr><td align="left"><code>\e[</code> 或 <code>\x1b[</code> 或 <code>\033[</code></td><td>开始颜色修改</td></tr><tr><td align="left"><code>COLORm</code></td><td>颜色代码 + <code>m</code> 在末尾</td></tr><tr><td align="left"><code>\e[0m</code> 或 <code>\x1b[0m</code> 或 <code>\033[0m</code></td><td>结束颜色修改</td></tr></tbody></table>
<!--rehype:className=left-align-->
<p><code>0x1b</code> 字面上称为 <code>ESC</code></p>
<h4 id="示例-1"><a aria-hidden="true" tabindex="-1" href="#示例-1"><span class="icon icon-link"></span></a>示例</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"<span class="token entity" title="\e">\e</span>[3m下划线文本<span class="token entity" title="\e">\e</span>[0m"</span>
</span></code></pre>
<p><code style="text-decoration: underline;color: inherit;">下划线文本</code><!--rehype:style=text-decoration: underline;color: inherit;--></p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"<span class="token entity" title="\e">\e</span>[1;33;4;44m粗体下划线蓝色背景黄色文字的文本<span class="token entity" title="\e">\e</span>[0m"</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
<p><code style="text-decoration: underline;color: inherit;font-weight: bold;color: #cdcd00;background: #0000ee;">粗体下划线蓝色背景黄色文字的文本</code><!--rehype:style=text-decoration: underline;color: inherit;font-weight: bold;color: #cdcd00;background: #0000ee;--></p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token punctuation">\</span>e<span class="token punctuation">[</span>         <span class="token comment"># 启用转义</span>
</span><span class="code-line"><span class="token number">1</span><span class="token punctuation">;</span><span class="token number">33</span><span class="token punctuation">;</span><span class="token number">4</span><span class="token punctuation">;</span><span class="token number">44</span>   <span class="token comment"># 参数 (1;33;4;44)</span>
</span><span class="code-line">m           <span class="token comment"># 设置图形模式</span>
</span></code></pre>
<h4 id="样式"><a aria-hidden="true" tabindex="-1" href="#样式"><span class="icon icon-link"></span></a>样式</h4>




























































<table class="left-align"><thead><tr><th align="left">:-</th><th>--</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>0</code></td><td>普通字符(复位或正常)</td><td>关闭所有属性</td></tr><tr><td align="left"><code>1</code></td><td><strong>粗体</strong>字</td><td></td></tr><tr><td align="left"><code>2</code></td><td>弱化(降低强度)</td><td>未广泛支持</td></tr><tr><td align="left"><code>3</code></td><td>斜体</td><td>未广泛支持有时为反相显示</td></tr><tr><td align="left"><code>4</code></td><td>下划线</td><td></td></tr><tr><td align="left"><code>5</code></td><td>缓慢闪烁</td><td></td></tr><tr><td align="left"><code>6</code></td><td>快速闪烁</td><td></td></tr><tr><td align="left"><code>7</code></td><td>反显</td><td></td></tr><tr><td align="left"><code>8</code></td><td>隐藏</td><td>未广泛支持。</td></tr><tr><td align="left"><code>9</code></td><td>划除</td><td></td></tr></tbody></table>
<!--rehype:className=left-align-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="ansi--颜色转义码"><a aria-hidden="true" tabindex="-1" href="#ansi--颜色转义码"><span class="icon icon-link"></span></a>ANSI — 颜色转义码</h3><div class="wrap-body">



























































<table class="show-header left-align"><thead><tr><th align="left">颜色</th><th>前景色</th><th>背景色</th><th>示例</th></tr></thead><tbody><tr><td align="left"><code>Black</code> 黑色</td><td>30</td><td>40</td><td>``<!--rehype:style=background:#000;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Red</code> 红色</td><td>31</td><td>41</td><td>``<!--rehype:style=background:#c23621;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Green</code> 绿色</td><td>32</td><td>42</td><td>``<!--rehype:style=background:#25bc26;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Yellow</code> 黄色</td><td>33</td><td>43</td><td>``<!--rehype:style=background:#cdcd00;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Blue</code> 蓝色</td><td>34</td><td>44</td><td>``<!--rehype:style=background:#0000ee;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Purple</code> 紫色</td><td>35</td><td>45</td><td>``<!--rehype:style=background:#cd00cd;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Cyan</code> 青色</td><td>36</td><td>46</td><td>``<!--rehype:style=background:#00AAAA;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>White</code> 白色</td><td>37</td><td>47</td><td>``<!--rehype:style=background:#e5e5e5;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr></tbody></table>
<!--rehype:className=show-header left-align-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="ansi--颜色转义码亮色"><a aria-hidden="true" tabindex="-1" href="#ansi--颜色转义码亮色"><span class="icon icon-link"></span></a>ANSI — 颜色转义码(亮色)</h3><div class="wrap-body">



























































<table class="show-header left-align"><thead><tr><th align="left">颜色</th><th>前景色</th><th>背景色</th><th>示例</th></tr></thead><tbody><tr><td align="left"><code>Bright Black(Gray)</code> 灰色</td><td>90</td><td>100</td><td>``<!--rehype:style=background:#555555;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright Red</code> 亮红色</td><td>91</td><td>101</td><td>``<!--rehype:style=background:#FF5555;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright Green</code> 亮绿色</td><td>92</td><td>102</td><td>``<!--rehype:style=background:#55FF55;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright Yellow</code> 亮黄色</td><td>93</td><td>103</td><td>``<!--rehype:style=background:#FFFF55;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright Blue</code> 亮蓝色</td><td>94</td><td>104</td><td>``<!--rehype:style=background:#5555FF;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright Magenta</code> 亮紫色</td><td>95</td><td>105</td><td>``<!--rehype:style=background:#FF55FF;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright Cyan</code> 亮青色</td><td>96</td><td>106</td><td>``<!--rehype:style=background:#55FFFF;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr><tr><td align="left"><code>Bright White</code> 浅灰</td><td>97</td><td>107</td><td>``<!--rehype:style=background:#ffffff;padding:0.2rem 1.2rem;border: 1px solid #333;--></td></tr></tbody></table>
<!--rehype:className=show-header left-align-->
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="可用功能"><a aria-hidden="true" tabindex="-1" href="#可用功能"><span class="icon icon-link"></span></a>可用功能</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->









































































































<table><thead><tr><th align="left">:-</th><th>--</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>n</code> A</td><td>光标上移</td><td>光标向指定的方向移动 <code>n</code>(默认1)格</td></tr><tr><td align="left"><code>n</code> B</td><td>光标下移</td><td>光标向指定的方向移动 <code>n</code>(默认1)格</td></tr><tr><td align="left"><code>n</code> C</td><td>光标前移</td><td>光标向指定的方向移动 <code>n</code>(默认1)格</td></tr><tr><td align="left"><code>n</code> D</td><td>光标后移</td><td>光标向指定的方向移动 <code>n</code>(默认1)格</td></tr><tr><td align="left"><code>n</code> E</td><td>光标移到下一行</td><td>光标移动到下面第 <code>n</code>(默认1)行的开头(非ANSI.SYS)</td></tr><tr><td align="left"><code>n</code> F</td><td>光标移到上一行</td><td>光标移动到上面第 <code>n</code>(默认1)行的开头(非ANSI.SYS)</td></tr><tr><td align="left"><code>n</code> G</td><td>光标水平绝对</td><td>光标移动到第 <code>n</code>(默认1)列(非ANSI.SYS)</td></tr><tr><td align="left"><code>n</code>;<code>m</code> H</td><td>光标位置</td><td>光标移动到第 <code>n</code>行、第 <code>m</code> 列。值从1开始，且默认为1(左上角)</td></tr><tr><td align="left"><code>n</code> J</td><td>ED – 擦除显示</td><td>清除屏幕的部分区域</td></tr><tr><td align="left"><code>n</code> K</td><td>EL – 擦除行</td><td>清除行内的部分区域</td></tr><tr><td align="left"><code>n</code> S</td><td>SU – 向上滚动</td><td>整页向上滚动 <code>n</code>(默认1)行。新行添加到底部 (非ANSI.SYS)</td></tr><tr><td align="left"><code>n</code> T</td><td>SD – 向下滚动</td><td>整页向下滚动 <code>n</code>(默认1)行。新行添加到顶部 (非ANSI.SYS)</td></tr><tr><td align="left"><code>n</code>;<code>m</code>f</td><td>HVP – 水平垂直位置</td><td>同CUP</td></tr><tr><td align="left"><code>n</code> <code>m</code></td><td>SGR – 选择图形再现</td><td>设置SGR参数，包括文字颜色</td></tr><tr><td align="left"><code>5i</code></td><td>打开辅助端口</td><td>启用辅助串行端口，通常用于本地串行打印机</td></tr><tr><td align="left"><code>4i</code></td><td>关闭辅助端口</td><td>禁用辅助串行端口，通常用于本地串行打印机</td></tr><tr><td align="left"><code>6n</code></td><td>DSR – 设备状态报告</td><td>以 <code>ESC[n;mR</code> (就像在键盘上输入)向应用程序报告光标位置(CPR)，其中 <code>n</code>是行， <code>m</code> 是列</td></tr><tr><td align="left"><code>s</code></td><td>SCP – 保存光标位置</td><td>保存光标的当前位置</td></tr><tr><td align="left"><code>u</code></td><td>RCP – 恢复光标位置</td><td>恢复保存的光标位置</td></tr></tbody></table>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://jaywcjlove.github.io/shell-tutorial">Shell 教程</a> <em>(jaywcjlove.github.io)</em></li>
<li><a href="https://devhints.io/bash">Devhints</a> <em>(devhints.io)</em></li>
<li><a href="http://wiki.bash-hackers.org/">Bash-hackers wiki</a> <em>(bash-hackers.org)</em></li>
<li><a href="http://wiki.bash-hackers.org/syntax/shellvars">Shell vars</a> <em>(bash-hackers.org)</em></li>
<li><a href="https://learnxinyminutes.com/docs/bash/">Learn bash in y minutes</a> <em>(learnxinyminutes.com)</em></li>
<li><a href="http://mywiki.wooledge.org/BashGuide">Bash Guide</a> <em>(mywiki.wooledge.org)</em></li>
<li><a href="https://www.shellcheck.net/">ShellCheck</a> <em>(shellcheck.net)</em></li>
<li><a href="https://devmanual.gentoo.org/tools-reference/bash/index.html">shell - Standard Shell</a> <em>(devmanual.gentoo.org)</em></li>
<li><a href="https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97">ANSI 转义序列</a> <em>(zh.wikipedia.org)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="..\/data.js?v=1.4.1" defer></script><script src="..\/js/fuse.min.js?v=1.4.1" defer></script><script src="..\/js/main.js?v=1.4.1" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
